﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

using InputLibs;
using System.Windows.Forms.DataVisualization.Charting;

struct _Calculation
{
    public double MinAngularity;
    public double MaxAngularity;
    public double AvgAngularity;
    public double AvgVelocity;
    public double AvgAcceleration;
    public double startTime;
    public double endTime;
    public double totalMovement;
    public double duration;
    public double AngularVelocity;
    public double AngularAcceleration;
};

namespace WalikingGraph
{
    public partial class Form1 : Form
    {
        private InputLab _input;
        Point? prevPosition1 = null;
        ToolTip tooltip1 = new ToolTip();

        private _Calculation RDiffFECalculation;
        private _Calculation RDiffABADCalculation;
        private _Calculation RUrFECalculation;
        private _Calculation RUrABADCalculation;
        private _Calculation RLrFECalculation;
        private _Calculation RLrABADCalculation;

        private _Calculation LDiffFECalculation;
        private _Calculation LDiffABADCalculation;
        private _Calculation LUrFECalculation;
        private _Calculation LUrABADCalculation;
        private _Calculation LLrFECalculation;
        private _Calculation LLrABADCalculation;

        static double selectedMinXChart1 = 0;
        static double selectedMaxXChart1 = 0;
        static double selectedMinXChart2 = 0;
        static double selectedMaxXChart2 = 0;

        private Boolean isLAutoMark = false;
        private Boolean isRAutoMark = false;

        private void displayRDiff_FE()
        {
            //RMinAng.Text = RDiffFECalculation.MinAngularity.ToString();
            //RMaxAng.Text = RDiffFECalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RDiffFECalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RDiffFECalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RDiffFECalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RDiffFECalculation.MinAngularity.ToString();
            REndAng.Text = RDiffFECalculation.MaxAngularity.ToString();
            RTimeStart.Text = RDiffFECalculation.startTime.ToString();
            RTimeEnd.Text = RDiffFECalculation.endTime.ToString();
            RTotalMovement.Text = RDiffFECalculation.totalMovement.ToString();
            RDuration.Text = RDiffFECalculation.duration.ToString();
            RAngularVel.Text = RDiffFECalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RDiffFECalculation.AngularAcceleration.ToString();
        }
        private void displayRDiff_ABAD()
        {
            //RMinAng.Text = RDiffABADCalculation.MinAngularity.ToString();
            //RMaxAng.Text = RDiffABADCalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RDiffABADCalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RDiffABADCalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RDiffABADCalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RDiffABADCalculation.MinAngularity.ToString();
            REndAng.Text = RDiffABADCalculation.MaxAngularity.ToString();
            RTimeStart.Text = RDiffABADCalculation.startTime.ToString();
            RTimeEnd.Text = RDiffABADCalculation.endTime.ToString();
            RTotalMovement.Text = RDiffABADCalculation.totalMovement.ToString();
            RDuration.Text = RDiffABADCalculation.duration.ToString();
            RAngularVel.Text = RDiffABADCalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RDiffABADCalculation.AngularAcceleration.ToString();
        }
        private void displayRUR_FE()
        {
            //RMinAng.Text = RUrFECalculation.MinAngularity.ToString();
            //RMaxAng.Text = RUrFECalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RUrFECalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RUrFECalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RUrFECalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RUrFECalculation.MinAngularity.ToString();
            REndAng.Text = RUrFECalculation.MaxAngularity.ToString();
            RTimeStart.Text = RUrFECalculation.startTime.ToString();
            RTimeEnd.Text = RUrFECalculation.endTime.ToString();
            RTotalMovement.Text = RUrFECalculation.totalMovement.ToString();
            RDuration.Text = RUrFECalculation.duration.ToString();
            RAngularVel.Text = RUrFECalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RUrFECalculation.AngularAcceleration.ToString();
        }
        private void displayRUR_ABAD()
        {
            //RMinAng.Text = RUrABADCalculation.MinAngularity.ToString();
            //RMaxAng.Text = RUrABADCalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RUrABADCalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RUrABADCalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RUrABADCalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RUrABADCalculation.MinAngularity.ToString();
            REndAng.Text = RUrABADCalculation.MaxAngularity.ToString();
            RTimeStart.Text = RUrABADCalculation.startTime.ToString();
            RTimeEnd.Text = RUrABADCalculation.endTime.ToString();
            RTotalMovement.Text = RUrABADCalculation.totalMovement.ToString();
            RDuration.Text = RUrABADCalculation.duration.ToString();
            RAngularVel.Text = RUrABADCalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RUrABADCalculation.AngularAcceleration.ToString();
        }
        private void displayRLR_FE()
        {
            //RMinAng.Text = RLrFECalculation.MinAngularity.ToString();
            //RMaxAng.Text = RLrFECalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RLrFECalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RLrFECalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RLrFECalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RLrFECalculation.MinAngularity.ToString();
            REndAng.Text = RLrFECalculation.MaxAngularity.ToString();
            RTimeStart.Text = RLrFECalculation.startTime.ToString();
            RTimeEnd.Text = RLrFECalculation.endTime.ToString();
            RTotalMovement.Text = RLrFECalculation.totalMovement.ToString();
            RDuration.Text = RLrFECalculation.duration.ToString();
            RAngularVel.Text = RLrFECalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RLrFECalculation.AngularAcceleration.ToString();
        }
        private void displayRLR_ABAD()
        {
            //RMinAng.Text = RLrABADCalculation.MinAngularity.ToString();
            //RMaxAng.Text = RLrABADCalculation.MaxAngularity.ToString();
            //RAvgAng.Text = RLrABADCalculation.AvgAngularity.ToString();

            //RAvgVel.Text = RLrABADCalculation.AvgVelocity.ToString();
            //RAvgAcc.Text = RLrABADCalculation.AvgAcceleration.ToString();

            //RMinVel.Text = "-";
            //RMaxVel.Text = "-";
            //RMinAcc.Text = "-";
            //RMaxAcc.Text = "-";

            RStartAng.Text = RLrABADCalculation.MinAngularity.ToString();
            REndAng.Text = RLrABADCalculation.MaxAngularity.ToString();
            RTimeStart.Text = RLrABADCalculation.startTime.ToString();
            RTimeEnd.Text = RLrABADCalculation.endTime.ToString();
            RTotalMovement.Text = RLrABADCalculation.totalMovement.ToString();
            RDuration.Text = RLrABADCalculation.duration.ToString();
            RAngularVel.Text = RLrABADCalculation.AngularVelocity.ToString();
            RAngularAcc.Text = RLrABADCalculation.AngularAcceleration.ToString();
        }


        private void displayLDiff_FE()
        {
            //LMinAng.Text = LDiffFECalculation.MinAngularity.ToString();
            //LMaxAng.Text = LDiffFECalculation.MaxAngularity.ToString();
            //LAvgAng.Text = LDiffFECalculation.AvgAngularity.ToString();

            //LAvgVel.Text = LDiffFECalculation.AvgVelocity.ToString();
            //LAvgAcc.Text = LDiffFECalculation.AvgAcceleration.ToString();

            //LMinVel.Text = "-";
            //LMaxVel.Text = "-";
            //LMinAcc.Text = "-";
            //LMaxAcc.Text = "-";
            LStartAng.Text = LDiffFECalculation.MinAngularity.ToString();
            LEndAng.Text = LDiffFECalculation.MaxAngularity.ToString();
            LTimeStart.Text = LDiffFECalculation.startTime.ToString();
            LTimeEnd.Text = LDiffFECalculation.endTime.ToString();
            LTotalMovement.Text = LDiffFECalculation.totalMovement.ToString();
            LDuration.Text = LDiffFECalculation.duration.ToString();
            LAngularVel.Text = LDiffFECalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LDiffFECalculation.AngularAcceleration.ToString();
        }
        private void displayLDiff_ABAD()
        {
            /*LMinAng.Text = LDiffABADCalculation.MinAngularity.ToString();
            //LMaxAng.Text = LDiffABADCalculation.MaxAngularity.ToString();
            //LAvgAng.Text = LDiffABADCalculation.AvgAngularity.ToString();

            LAvgVel.Text = LDiffABADCalculation.AvgVelocity.ToString();
            LAvgAcc.Text = LDiffABADCalculation.AvgAcceleration.ToString();

            LMinVel.Text = "-";
            LMaxVel.Text = "-";
            LMinAcc.Text = "-";
            LMaxAcc.Text = "-";*/
            LStartAng.Text = LDiffABADCalculation.MinAngularity.ToString();
            LEndAng.Text = LDiffABADCalculation.MaxAngularity.ToString();
            LTimeStart.Text = LDiffABADCalculation.startTime.ToString();
            LTimeEnd.Text = LDiffABADCalculation.endTime.ToString();
            LTotalMovement.Text = LDiffABADCalculation.totalMovement.ToString();
            LDuration.Text = LDiffABADCalculation.duration.ToString();
            LAngularVel.Text = LDiffABADCalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LDiffABADCalculation.AngularAcceleration.ToString();
        }
        private void displayLUR_FE()
        {
           /* LMinAng.Text = LUrFECalculation.MinAngularity.ToString();
            LMaxAng.Text = LUrFECalculation.MaxAngularity.ToString();
            LAvgAng.Text = LUrFECalculation.AvgAngularity.ToString();

            LAvgVel.Text = LUrFECalculation.AvgVelocity.ToString();
            LAvgAcc.Text = LUrFECalculation.AvgAcceleration.ToString();

            LMinVel.Text = "-";
            LMaxVel.Text = "-";
            LMinAcc.Text = "-";
            LMaxAcc.Text = "-";*/
            LStartAng.Text = LUrFECalculation.MinAngularity.ToString();
            LEndAng.Text = LUrFECalculation.MaxAngularity.ToString();
            LTimeStart.Text = LUrFECalculation.startTime.ToString();
            LTimeEnd.Text = LUrFECalculation.endTime.ToString();
            LTotalMovement.Text = LUrFECalculation.totalMovement.ToString();
            LDuration.Text = LUrFECalculation.duration.ToString();
            LAngularVel.Text = LUrFECalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LUrFECalculation.AngularAcceleration.ToString();
        }
        private void displayLUR_ABAD()
        {
           /* LMinAng.Text = LUrABADCalculation.MinAngularity.ToString();
            LMaxAng.Text = LUrABADCalculation.MaxAngularity.ToString();
            LAvgAng.Text = LUrABADCalculation.AvgAngularity.ToString();

            LAvgVel.Text = LUrABADCalculation.AvgVelocity.ToString();
            LAvgAcc.Text = LUrABADCalculation.AvgAcceleration.ToString();

            LMinVel.Text = "-";
            LMaxVel.Text = "-";
            LMinAcc.Text = "-";
            LMaxAcc.Text = "-";*/
            LStartAng.Text = LUrABADCalculation.MinAngularity.ToString();
            LEndAng.Text = LUrABADCalculation.MaxAngularity.ToString();
            LTimeStart.Text = LUrABADCalculation.startTime.ToString();
            LTimeEnd.Text = LUrABADCalculation.endTime.ToString();
            LTotalMovement.Text = LUrABADCalculation.totalMovement.ToString();
            LDuration.Text = LUrABADCalculation.duration.ToString();
            LAngularVel.Text = LUrABADCalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LUrABADCalculation.AngularAcceleration.ToString();
        }
        private void displayLLR_FE()
        {
           /* LMinAng.Text = LLrFECalculation.MinAngularity.ToString();
            LMaxAng.Text = LLrFECalculation.MaxAngularity.ToString();
            LAvgAng.Text = LLrFECalculation.AvgAngularity.ToString();

            LAvgVel.Text = LLrFECalculation.AvgVelocity.ToString();
            LAvgAcc.Text = LLrFECalculation.AvgAcceleration.ToString();

            LMinVel.Text = "-";
            LMaxVel.Text = "-";
            LMinAcc.Text = "-";
            LMaxAcc.Text = "-";*/
            LStartAng.Text = LLrFECalculation.MinAngularity.ToString();
            LEndAng.Text = LLrFECalculation.MaxAngularity.ToString();
            LTimeStart.Text = LLrFECalculation.startTime.ToString();
            LTimeEnd.Text = LLrFECalculation.endTime.ToString();
            LTotalMovement.Text = LLrFECalculation.totalMovement.ToString();
            LDuration.Text = LLrFECalculation.duration.ToString();
            LAngularVel.Text = LLrFECalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LLrFECalculation.AngularAcceleration.ToString();
        }
        private void displayLLR_ABAD()
        {
          /*  LMinAng.Text = LLrABADCalculation.MinAngularity.ToString();
            LMaxAng.Text = LLrABADCalculation.MaxAngularity.ToString();
            LAvgAng.Text = LLrABADCalculation.AvgAngularity.ToString();

            LAvgVel.Text = LLrABADCalculation.AvgVelocity.ToString();
            LAvgAcc.Text = LLrABADCalculation.AvgAcceleration.ToString();

            LMinVel.Text = "-";
            LMaxVel.Text = "-";
            LMinAcc.Text = "-";
            LMaxAcc.Text = "-";*/
            LStartAng.Text = LLrABADCalculation.MinAngularity.ToString();
            LEndAng.Text = LLrABADCalculation.MaxAngularity.ToString();
            LTimeStart.Text = LLrABADCalculation.startTime.ToString();
            LTimeEnd.Text = LLrABADCalculation.endTime.ToString();
            LTotalMovement.Text = LLrABADCalculation.totalMovement.ToString();
            LDuration.Text = LLrABADCalculation.duration.ToString();
            LAngularVel.Text = LLrABADCalculation.AngularVelocity.ToString();
            LAngularAcc.Text = LLrABADCalculation.AngularAcceleration.ToString();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileInput1.ShowDialog();

            _input.FirstFileStr = openFileInput1.FileName;

            textBox1.Text = openFileInput1.SafeFileName.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            _input.ReadRight();

            inputRowBindingSource.DataSource = _input.GetRight();

            chart1.DataBind();

            double yMin;
            double yMax;
            yMin = this.chart1.Series[0].Points.Min(x => x.YValues[0]);
            yMax = this.chart1.Series[0].Points.Max(x => x.YValues[0]);

            //RStartAng.Text = yMin.ToString();
            //REndAng.Text = yMax.ToString();
            //RAvgAng.Text = ((yMin + yMax) / 2).ToString();

           // RTotalMovement.Text = "-";
           // RAngularAcc.Text = "-";
           // RAngularVel.Text = "-";

           // RDuration.Text = "-";
            //RMaxAcc.Text = "-";
            //RAvgAcc.Text = "-";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _input = new InputLab();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileInput2.ShowDialog();

            _input.SecondFileStr = openFileInput2.FileName;

            textBox2.Text = openFileInput2.SafeFileName.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileInput3.ShowDialog();

            _input.ThirdFileStr = openFileInput3.FileName;

            textBox3.Text = openFileInput3.SafeFileName.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            openFileInput4.ShowDialog();

            _input.ForthFileStr = openFileInput4.FileName;

            textBox4.Text = openFileInput4.SafeFileName.ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            openFileInput5.ShowDialog();

            _input.FiFthFileStr = openFileInput5.FileName;

            textBox5.Text = openFileInput5.SafeFileName.ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            openFileInput6.ShowDialog();

            _input.SixthFileStr = openFileInput6.FileName;

            textBox6.Text = openFileInput6.SafeFileName.ToString();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            _input.ReadLeft();

            inputRowLeftBindingSource.DataSource = _input.GetLeft();

            chart2.DataBind();
            double yMin;
            double yMax;
            yMin = this.chart2.Series[0].Points.Min(x => x.YValues[0]);
            yMax = this.chart2.Series[0].Points.Max(x => x.YValues[0]);

            if (RDiff_FE.Checked)
            {
                //LMinAng.Text = yMin.ToString();
                //LMaxAng.Text = yMax.ToString();
                //LAvgAng.Text = ((yMin + yMax) / 2).ToString();
            }
        }

        private void chart1_SelectionRangeChanged(object sender, System.Windows.Forms.DataVisualization.Charting.CursorEventArgs e)
        {
            double minX, maxX, minY = 0.0, maxY = 0.0, startY = 0.0, endY = 0.0;

            if (e.NewSelectionEnd > e.NewSelectionStart)
            {
                minX = (e.NewSelectionStart);
                maxX = (e.NewSelectionEnd);
            }
            else
            {
                minX = (e.NewSelectionEnd);
                maxX = (e.NewSelectionStart);
            }

            selectedMinXChart1 = e.NewSelectionStart;
            selectedMaxXChart1 = e.NewSelectionEnd;

            // RDiffFECalculation
            {
                DataPointCollection points = chart1.Series[0].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    RDiffFECalculation.MinAngularity = startY;
                    RDiffFECalculation.MaxAngularity = endY;
                    RDiffFECalculation.startTime = selectedMinXChart1;
                    RDiffFECalculation.endTime = selectedMaxXChart1;
                    RDiffFECalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RDiffFECalculation.totalMovement = endY - startY;
                    RDiffFECalculation.AngularVelocity = RDiffFECalculation.totalMovement / RDiffFECalculation.duration;
                    RDiffFECalculation.AngularAcceleration = RDiffFECalculation.totalMovement / Math.Pow(RDiffFECalculation.duration, 2);

                }
                //RDiffFECalculation.MaxAngularity = maxY;
                //RDiffFECalculation.AvgAngularity = (minY + maxY) / 2;
                //RDiffFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                //RDiffFECalculation.AvgAcceleration = RDiffFECalculation.AvgVelocity / (maxX - minX);
            }
            // RDiffABADCalculation
//            if (chart1.Series[1].Enabled)
            {

                DataPointCollection points = chart1.Series[1].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;
                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];


                    RDiffABADCalculation.MinAngularity = startY;
                    RDiffABADCalculation.MaxAngularity = endY;
                    RDiffABADCalculation.startTime = selectedMinXChart1;
                    RDiffABADCalculation.endTime = selectedMaxXChart1;
                    RDiffABADCalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RDiffABADCalculation.totalMovement = endY - startY;
                    RDiffABADCalculation.AngularVelocity = RDiffABADCalculation.totalMovement / RDiffABADCalculation.duration;
                    RDiffABADCalculation.AngularAcceleration = RDiffABADCalculation.totalMovement / Math.Pow(RDiffABADCalculation.duration, 2);

                }
                //RDiffABADCalculation.MinAngularity = minY;
                //RDiffABADCalculation.MaxAngularity = maxY;
                //RDiffABADCalculation.AvgAngularity = (minY + maxY) / 2;
                //RDiffABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                //RDiffABADCalculation.AvgAcceleration = RDiffABADCalculation.AvgVelocity / (maxX - minX);
            }
            // RUrFECalculation
//            if (chart1.Series[3].Enabled)
            {
                DataPointCollection points = chart1.Series[3].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;
                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];


                    //RUrFECalculation.MinAngularity = minY;
                    //RUrFECalculation.MaxAngularity = maxY;
                    //RUrFECalculation.AvgAngularity = (minY + maxY) / 2;
                    //RUrFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //RUrFECalculation.AvgAcceleration = RUrFECalculation.AvgVelocity / (maxX - minX);

                    RUrFECalculation.MinAngularity = startY;
                    RUrFECalculation.MaxAngularity = endY;
                    RUrFECalculation.startTime = selectedMinXChart1;
                    RUrFECalculation.endTime = selectedMaxXChart1;
                    RUrFECalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RUrFECalculation.totalMovement = endY - startY;
                    RUrFECalculation.AngularVelocity = RUrFECalculation.totalMovement / RUrFECalculation.duration;
                    RUrFECalculation.AngularAcceleration = RUrFECalculation.totalMovement / Math.Pow(RUrFECalculation.duration, 2);
                }
            }
            // RUrABADCalculation
//            if (chart1.Series[4].Enabled)
            {
                DataPointCollection points = chart1.Series[4].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;
                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //RUrABADCalculation.MinAngularity = minY;
                    //RUrABADCalculation.MaxAngularity = maxY;
                    //RUrABADCalculation.AvgAngularity = (minY + maxY) / 2;
                    //RUrABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //RUrABADCalculation.AvgAcceleration = RUrABADCalculation.AvgVelocity / (maxX - minX);

                    RUrABADCalculation.MinAngularity = startY;
                    RUrABADCalculation.MaxAngularity = endY;
                    RUrABADCalculation.startTime = selectedMinXChart1;
                    RUrABADCalculation.endTime = selectedMaxXChart1;
                    RUrABADCalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RUrABADCalculation.totalMovement = endY - startY;
                    RUrABADCalculation.AngularVelocity = RUrABADCalculation.totalMovement / RUrABADCalculation.duration;
                    RUrABADCalculation.AngularAcceleration = RUrABADCalculation.totalMovement / Math.Pow(RUrABADCalculation.duration, 2);
                }
             }
            // RLrFECalculation
//            if (chart1.Series[6].Enabled)
            {
                DataPointCollection points = chart1.Series[6].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];


                    //RLrFECalculation.MinAngularity = minY;
                    //RLrFECalculation.MaxAngularity = maxY;
                    //RLrFECalculation.AvgAngularity = (minY + maxY) / 2;
                    //RLrFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //RLrFECalculation.AvgAcceleration = RLrFECalculation.AvgVelocity / (maxX - minX);

                    RLrFECalculation.MinAngularity = startY;
                    RLrFECalculation.MaxAngularity = endY;
                    RLrFECalculation.startTime = selectedMinXChart1;
                    RLrFECalculation.endTime = selectedMaxXChart1;
                    RLrFECalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RLrFECalculation.totalMovement = endY - startY;
                    RLrFECalculation.AngularVelocity = RLrFECalculation.totalMovement / RLrFECalculation.duration;
                    RLrFECalculation.AngularAcceleration = RLrFECalculation.totalMovement / Math.Pow(RLrFECalculation.duration, 2);
                }
            }
            // RLrABADCalculation
//            if (chart1.Series[7].Enabled)
            {
                DataPointCollection points = chart1.Series[6].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;
                if (range.Count() > 1)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    // RLrABADCalculation.MinAngularity = minY;
                    // RLrABADCalculation.MaxAngularity = maxY;
                    // RLrABADCalculation.AvgAngularity = (minY + maxY) / 2;
                    // RLrABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    // RLrABADCalculation.AvgAcceleration = RLrABADCalculation.AvgVelocity / (maxX - minX);

                    RLrABADCalculation.MinAngularity = startY;
                    RLrABADCalculation.MaxAngularity = endY;
                    RLrABADCalculation.startTime = selectedMinXChart1;
                    RLrABADCalculation.endTime = selectedMaxXChart1;
                    RLrABADCalculation.duration = selectedMaxXChart1 - selectedMinXChart1;
                    RLrABADCalculation.totalMovement = endY - startY;
                    RLrABADCalculation.AngularVelocity = RLrABADCalculation.totalMovement / RLrABADCalculation.duration;
                    RLrABADCalculation.AngularAcceleration = RLrABADCalculation.totalMovement / Math.Pow(RLrABADCalculation.duration, 2);
                }
            }
            if (RDiff_FE.Checked)
            {
                displayRDiff_FE();
            }
            else if (RDiff_ABAD.Checked)
            {
                displayRDiff_ABAD();
            }
            else if (RUR_FE.Checked)
            {
                displayRUR_FE();
            }
            else if (RUR_ABAD.Checked)
            {
                displayRUR_ABAD();
            }
            else if (RLR_FE.Checked)
            {
                displayRLR_FE();
            }
            else if (RLR_ABAD.Checked)
            {
                displayRLR_ABAD();
            }

        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.Location;
            if (prevPosition1.HasValue && pos == prevPosition1.Value)
                return;
            tooltip1.RemoveAll();
            prevPosition1 = pos;
            var results = chart1.HitTest(pos.X, pos.Y, false,
                                            ChartElementType.DataPoint);
            
            foreach (var result in results)
            {
                if (result.ChartElementType == ChartElementType.DataPoint)
                {
                    var prop = result.Object as DataPoint;
                    if (prop != null)
                    {
                        var pointXPixel = result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
                        var pointYPixel = result.ChartArea.AxisY.ValueToPixelPosition(prop.YValues[0]);

                        // check if the cursor is really close to the point (2 pixels around the point)
                        if (Math.Abs(pos.X - pointXPixel) < 2 &&
                            Math.Abs(pos.Y - pointYPixel) < 2)
                        {
                            tooltip1.Show("X=" + prop.XValue + ", Y=" + prop.YValues[0], this.chart1,
                                            pos.X, pos.Y - 15);
                        }
                    }
                }
            }
        }

        private void chart2_SelectionRangeChanged(object sender, CursorEventArgs e)
        {
            double minX, maxX, minY = 0.0, maxY = 0.0, startY = 0.0, endY = 0.0;

            if (e.NewSelectionEnd > e.NewSelectionStart)
            {
                minX = (e.NewSelectionStart);
                maxX = (e.NewSelectionEnd);
            }
            else
            {
                minX = (e.NewSelectionEnd);
                maxX = (e.NewSelectionStart);
            }
            selectedMinXChart2 = e.NewSelectionStart;
            selectedMaxXChart2 = e.NewSelectionEnd;

            // LDiffFECalculation
            {
                DataPointCollection points = chart1.Series[0].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    // LDiffFECalculation.MinAngularity = minY;
                    // LDiffFECalculation.MaxAngularity = maxY;
                    // LDiffFECalculation.AvgAngularity = (minY + maxY) / 2;
                    // LDiffFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    // LDiffFECalculation.AvgAcceleration = LDiffFECalculation.AvgVelocity / (maxX - minX);

                    LDiffFECalculation.MinAngularity = startY;
                    LDiffFECalculation.MaxAngularity = endY;
                    LDiffFECalculation.startTime = selectedMinXChart2;
                    LDiffFECalculation.endTime = selectedMaxXChart2;
                    LDiffFECalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LDiffFECalculation.totalMovement = endY - startY;
                    LDiffFECalculation.AngularVelocity = LDiffFECalculation.totalMovement / LDiffFECalculation.duration;
                    LDiffFECalculation.AngularAcceleration = LDiffFECalculation.totalMovement / Math.Pow(LDiffFECalculation.duration, 2);
                }
            }
            // LDiffABADCalculation
//            if (chart2.Series[1].Enabled)
            {
                DataPointCollection points = chart1.Series[1].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //LDiffABADCalculation.MinAngularity = minY;
                    //LDiffABADCalculation.MaxAngularity = maxY;
                    //LDiffABADCalculation.AvgAngularity = (minY + maxY) / 2;
                    //LDiffABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //LDiffABADCalculation.AvgAcceleration = LDiffABADCalculation.AvgVelocity / (maxX - minX);
                    LDiffABADCalculation.MinAngularity = startY;
                    LDiffABADCalculation.MaxAngularity = endY;
                    LDiffABADCalculation.startTime = selectedMinXChart2;
                    LDiffABADCalculation.endTime = selectedMaxXChart2;
                    LDiffABADCalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LDiffABADCalculation.totalMovement = endY - startY;
                    LDiffABADCalculation.AngularVelocity = LDiffABADCalculation.totalMovement / LDiffABADCalculation.duration;
                    LDiffABADCalculation.AngularAcceleration = LDiffABADCalculation.totalMovement / Math.Pow(LDiffABADCalculation.duration, 2);
                }
            }
            // LUrFECalculation
//            if (chart2.Series[3].Enabled)
            {
                DataPointCollection points = chart1.Series[3].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //LUrFECalculation.MinAngularity = minY;
                    //LUrFECalculation.MaxAngularity = maxY;
                    //LUrFECalculation.AvgAngularity = (minY + maxY) / 2;
                    //LUrFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //LUrFECalculation.AvgAcceleration = LUrFECalculation.AvgVelocity / (maxX - minX);
                    LUrFECalculation.MinAngularity = startY;
                    LUrFECalculation.MaxAngularity = endY;
                    LUrFECalculation.startTime = selectedMinXChart2;
                    LUrFECalculation.endTime = selectedMaxXChart2;
                    LUrFECalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LUrFECalculation.totalMovement = endY - startY;
                    LUrFECalculation.AngularVelocity = LUrFECalculation.totalMovement / LUrFECalculation.duration;
                    LUrFECalculation.AngularAcceleration = LUrFECalculation.totalMovement / Math.Pow(LUrFECalculation.duration, 2);
                }
            }
            // LUrABADCalculation
//            if (chart2.Series[4].Enabled)
            {
                DataPointCollection points = chart1.Series[4].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //LUrABADCalculation.MinAngularity = minY;
                    //LUrABADCalculation.MaxAngularity = maxY;
                    //LUrABADCalculation.AvgAngularity = (minY + maxY) / 2;
                    ///LUrABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //LUrABADCalculation.AvgAcceleration = LUrABADCalculation.AvgVelocity / (maxX - minX);
                    LUrABADCalculation.MinAngularity = startY;
                    LUrABADCalculation.MaxAngularity = endY;
                    LUrABADCalculation.startTime = selectedMinXChart2;
                    LUrABADCalculation.endTime = selectedMaxXChart2;
                    LUrABADCalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LUrABADCalculation.totalMovement = endY - startY;
                    LUrABADCalculation.AngularVelocity = LUrABADCalculation.totalMovement / LUrABADCalculation.duration;
                    LUrABADCalculation.AngularAcceleration = LUrABADCalculation.totalMovement / Math.Pow(LUrABADCalculation.duration, 2);
                }
            }
            // LLrFECalculation
//            if (chart2.Series[6].Enabled)
            {
                DataPointCollection points = chart1.Series[6].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //LLrFECalculation.MinAngularity = minY;
                    //LLrFECalculation.MaxAngularity = maxY;
                    //LLrFECalculation.AvgAngularity = (minY + maxY) / 2;
                    //LLrFECalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //LLrFECalculation.AvgAcceleration = LLrFECalculation.AvgVelocity / (maxX - minX);
                    LLrFECalculation.MinAngularity = startY;
                    LLrFECalculation.MaxAngularity = endY;
                    LLrFECalculation.startTime = selectedMinXChart2;
                    LLrFECalculation.endTime = selectedMaxXChart2;
                    LLrFECalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LLrFECalculation.totalMovement = endY - startY;
                    LLrFECalculation.AngularVelocity = LLrFECalculation.totalMovement / LLrFECalculation.duration;
                    LLrFECalculation.AngularAcceleration = LLrFECalculation.totalMovement / Math.Pow(LLrFECalculation.duration, 2);
                }
            }
            // LLrABADCalculation
//            if (chart2.Series[7].Enabled)
            {
                DataPointCollection points = chart1.Series[6].Points;
                IEnumerable<DataPoint> range =
                    from p in points
                    where p.XValue > minX && p.XValue < maxX
                    select p;

                if (range.Count() > 0)
                {
                    DataPoint dp = range.First<DataPoint>();
                    minY = dp.YValues[0];
                    maxY = dp.YValues[0];

                    startY = dp.YValues[0];
                    dp = range.Last<DataPoint>();
                    endY = dp.YValues[0];

                    //LLrABADCalculation.MinAngularity = minY;
                    //LLrABADCalculation.MaxAngularity = maxY;
                    //LLrABADCalculation.AvgAngularity = (minY + maxY) / 2;
                    //LLrABADCalculation.AvgVelocity = (maxX - minX) / (endY - startY);
                    //LLrABADCalculation.AvgAcceleration = LLrABADCalculation.AvgVelocity / (maxX - minX);
                    LLrABADCalculation.MinAngularity = startY;
                    LLrABADCalculation.MaxAngularity = endY;
                    LLrABADCalculation.startTime = selectedMinXChart2;
                    LLrABADCalculation.endTime = selectedMaxXChart2;
                    LLrABADCalculation.duration = selectedMaxXChart2 - selectedMinXChart2;
                    LLrABADCalculation.totalMovement = endY - startY;
                    LLrABADCalculation.AngularVelocity = LLrABADCalculation.totalMovement / LLrABADCalculation.duration;
                    LLrABADCalculation.AngularAcceleration = LLrABADCalculation.totalMovement / Math.Pow(LLrABADCalculation.duration, 2);
                }
            }
            if (LDiff_FE.Checked)
            {
                displayLDiff_FE();
            }
            else if (LDiff_ABAD.Checked)
            {
                displayRDiff_ABAD();
            }
            else if (LUR_FE.Checked)
            {
                displayLUR_FE();
            }
            else if (LUR_ABAD.Checked)
            {
                displayLUR_ABAD();
            }
            else if (LLR_FE.Checked)
            {
                displayLLR_FE();
            }
            else if (LLR_ABAD.Checked)
            {
                displayLLR_ABAD();
            }
        }

        private void chart2_MouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.Location;
            if (prevPosition1.HasValue && pos == prevPosition1.Value)
                return;
            tooltip1.RemoveAll();
            prevPosition1 = pos;
            var results = chart2.HitTest(pos.X, pos.Y, false,
                                            ChartElementType.DataPoint);
            foreach (var result in results)
            {
                if (result.ChartElementType == ChartElementType.DataPoint)
                {
                    var prop = result.Object as DataPoint;
                    if (prop != null)
                    {
                        var pointXPixel = result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
                        var pointYPixel = result.ChartArea.AxisY.ValueToPixelPosition(prop.YValues[0]);

                        // check if the cursor is really close to the point (2 pixels around the point)
                        if (Math.Abs(pos.X - pointXPixel) < 2 &&
                            Math.Abs(pos.Y - pointYPixel) < 2)
                        {
                            tooltip1.Show("X=" + prop.XValue + ", Y=" + prop.YValues[0], this.chart2,
                                            pos.X, pos.Y - 15);
                        }
                    }
                }
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (RDiff_FE.Checked)
            {
                MessageBox.Show("XX");
            }
        }



        private void label15_Click_1(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            displayRUR_FE();
        }

        private void RDiff_FE_CheckedChanged(object sender, EventArgs e)
        {
            displayRDiff_FE();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[1].Enabled = checkBox2.Checked;
        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }

        private void button3_Click_1(object sender, EventArgs e)
        {

        }

        private void button7_Click_1(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {

        }

        private void button5_Click_1(object sender, EventArgs e)
        {

        }

        private void button6_Click_1(object sender, EventArgs e)
        {

        }

        private void button8_Click_1(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[2].Enabled = checkBox3.Checked;
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[4].Enabled = checkBox5.Checked;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[5].Enabled = checkBox4.Checked;
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[7].Enabled = checkBox8.Checked;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[8].Enabled = checkBox7.Checked;
        }

        private void chart2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[1].Enabled = checkBox11.Checked;
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[2].Enabled = checkBox10.Checked;
        }

        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[4].Enabled = checkBox14.Checked;
        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[5].Enabled = checkBox13.Checked;
        }

        private void checkBox17_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[7].Enabled = checkBox17.Checked;
        }

        private void checkBox16_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[8].Enabled = checkBox16.Checked;
        }

        private void RDiff_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayRDiff_ABAD();
        }

        private void RUR_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayRUR_ABAD();
        }

        private void RLR_FE_CheckedChanged(object sender, EventArgs e)
        {
            displayRLR_FE();
        }

        private void RLR_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayRLR_ABAD();
        }

        private void LDiff_FE_CheckedChanged(object sender, EventArgs e)
        {
            displayLDiff_FE();
        }

        private void LDiff_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayLDiff_ABAD();
        }

        private void LUR_FE_CheckedChanged(object sender, EventArgs e)
        {
            displayLUR_FE();
        }

        private void LUR_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayLUR_ABAD();
        }

        private void LLR_FE_CheckedChanged(object sender, EventArgs e)
        {
            displayLLR_FE();
        }

        private void LLR_ABAD_CheckedChanged(object sender, EventArgs e)
        {
            displayLLR_ABAD();
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button9_Click_1(object sender, EventArgs e)
        {
            //if (selectedMaxXChart1 != selectedMinXChart1)
            {
                for (int i = 0; i < 1; i++)
                {                    
                    if (this.chart1.Series[i].Points.Count > 0)
                    {
                        int count = 0;
                        double previousX, previousY, currentX, currentY;
                        Boolean isIncrease = true;
                        previousX = 0;
                        previousY = 0;

                        foreach (DataPoint dataPoint in this.chart1.Series[i].Points)
                        {
                            //MessageBox.Show(dataPoint.XValue.ToString() + (dataPoint.YValues[0].ToString()));

                            currentX = dataPoint.XValue;
                            currentY = dataPoint.YValues[0];

                            if (count == 0)
                            {

                            }
                            else if (count == 1)
                            {
                                if (currentY > previousY)
                                {
                                    isIncrease = true;
                                }
                                else if (currentY < previousY)
                                {
                                    isIncrease = true;
                                }
                            }
                            else
                            {
                                if ((currentY > previousY) && isIncrease == true)
                                {
                                    isIncrease = true;
                                }
                                //
                                else if ((currentY < previousY) && isIncrease == true)
                                {
                                    //previouse is max
                                    //if ((currentX >= selectedMinXChart1) && (currentX <= selectedMaxXChart1))
                                    {
                                        if (!isRAutoMark)
                                        {
                                            chart1.Series[i].Points[count].MarkerStyle = MarkerStyle.Circle;
                                            chart1.Series[i].Points[count].MarkerSize = 3;
                                            chart1.Series[i].Points[count].MarkerColor = Color.White;
                                            chart1.Series[i].Points[count].MarkerBorderColor = Color.Black;
                                        }
                                        else
                                        {
                                            chart1.Series[i].Points[count].MarkerStyle = MarkerStyle.None;
                                        }
                                    }
                                    isIncrease = false;
                                }
                                else if ((currentY < previousY) && isIncrease == false)
                                {
                                    isIncrease = false;
                                }
                                else if ((currentY > previousY) && isIncrease == false)
                                {
                                    //previouse is min
                                    //if ((currentX >= selectedMinXChart1) && (currentX <= selectedMaxXChart1))
                                    {
                                        if (!isRAutoMark)
                                        {
                                            chart1.Series[i].Points[count].MarkerStyle = MarkerStyle.Circle;
                                            chart1.Series[i].Points[count].MarkerSize = 3;
                                            chart1.Series[i].Points[count].MarkerColor = Color.White;
                                            chart1.Series[i].Points[count].MarkerBorderColor = Color.Black;
                                        }
                                        else
                                        {
                                            chart1.Series[i].Points[count].MarkerStyle = MarkerStyle.None;
                                        }
                                        isIncrease = true;
                                    }
                                }

                                previousX = currentX;
                                previousY = currentY;
                            }
                            count++;
                        }
                    }
                }
                isRAutoMark = !isRAutoMark;
            }
            //else
            {
                //MessageBox.Show("please select area.");
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //if (selectedMaxXChart2 != selectedMinXChart2)
            {
                for (int i = 0; i < 1; i++)
                {
                    if (this.chart2.Series[i].Points.Count > 0)
                    {
                        int count = 0;
                        double previousX, previousY, currentX, currentY;
                        Boolean isIncrease = true;
                        previousX = 0;
                        previousY = 0;

                        foreach (DataPoint dataPoint in this.chart2.Series[i].Points)
                        {
                            //MessageBox.Show(dataPoint.XValue.ToString() + (dataPoint.YValues[0].ToString()));

                            currentX = dataPoint.XValue;
                            currentY = dataPoint.YValues[0];

                            if (count == 0)
                            {

                            }
                            else if (count == 1)
                            {
                                if (currentY > previousY)
                                {
                                    isIncrease = true;
                                }
                                else if (currentY < previousY)
                                {
                                    isIncrease = true;
                                }
                            }
                            else
                            {
                                if ((currentY > previousY) && isIncrease == true)
                                {
                                    isIncrease = true;
                                }
                                //
                                else if ((currentY < previousY) && isIncrease == true)
                                {
                                    //previouse is max
                                    //if ((currentX >= selectedMinXChart2) && (currentX <= selectedMaxXChart2))
                                    {
                                        if (!isLAutoMark)
                                        {
                                            chart2.Series[i].Points[count].MarkerStyle = MarkerStyle.Circle;
                                            chart2.Series[i].Points[count].MarkerSize = 3;
                                            chart2.Series[i].Points[count].MarkerColor = Color.White;
                                            chart2.Series[i].Points[count].MarkerBorderColor = Color.Black;
                                        }
                                        else
                                        {
                                            chart2.Series[i].Points[count].MarkerStyle = MarkerStyle.None;
                                        }
                                    }
                                    isIncrease = false;
                                }
                                else if ((currentY < previousY) && isIncrease == false)
                                {
                                    isIncrease = false;
                                }
                                else if ((currentY > previousY) && isIncrease == false)
                                {
                                    //previouse is min
                                    //if ((currentX >= selectedMinXChart2) && (currentX <= selectedMaxXChart2))
                                    {
                                        if (!isLAutoMark)
                                        {
                                            chart2.Series[i].Points[count].MarkerStyle = MarkerStyle.Circle;
                                            chart2.Series[i].Points[count].MarkerSize = 3;
                                            chart2.Series[i].Points[count].MarkerColor = Color.White;
                                            chart2.Series[i].Points[count].MarkerBorderColor = Color.Black;
                                        }
                                        else
                                        {
                                            chart2.Series[i].Points[count].MarkerStyle = MarkerStyle.None;
                                        }
                                        isIncrease = true;
                                    }
                                }

                                previousX = currentX;
                                previousY = currentY;
                            }
                            count++;
                        }
                    }
                }
                isLAutoMark = !isLAutoMark;                
            }
            //else
            {
                //MessageBox.Show("please select area.");
            }
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (textBox9.Text != "")
            {
                ScreenShotDemo.ScreenCapture sc = new ScreenShotDemo.ScreenCapture();
                Image img = sc.CaptureScreen();
                sc.CaptureWindowToFile(this.Handle, "C:\\reportCapture2\\" + textBox9.Text + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                MessageBox.Show("Already save to " + textBox9.Text + ".jpeg");
            }
            else
            {
                MessageBox.Show("please fill ID number.");
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[3].Enabled = checkBox6.Checked;
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            chart1.Series[6].Enabled = checkBox9.Checked;
        }

        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[3].Enabled = checkBox15.Checked;
        }

        private void checkBox18_CheckedChanged(object sender, EventArgs e)
        {
            chart2.Series[6].Enabled = checkBox18.Checked;
        }

        //Form2 settings = new Form2();
        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox9.Text != "")
            {
                //settings.Show();

                // read properties from form once it's closed
                if (!dir_check())
                {
                    dir_create();
                }

                ScreenShotDemo.ScreenCapture sc = new ScreenShotDemo.ScreenCapture();
                Image img = sc.CaptureScreen();
                sc.CaptureWindowToFile(this.Handle, "C:\\kneeAngleAnalyzer\\reportCapture\\" + textBox9.Text + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                MessageBox.Show("Already save to C:\\kneeAngleAnalyzer\\reportCapture\\" + textBox9.Text + ".jpeg");
            }
            else
            {
                MessageBox.Show("please fill ID number.");
            }
        }

        private void dir_create()
        {
            //string dirname = TextBox1.Text;
            // Reading directory name from a textbox in the form
            DirectoryInfo dir1 = new DirectoryInfo("C:\\kneeAngleAnalyzer\\reportCapture");
            dir1.Create();
        }

        private Boolean dir_check()
        {
          //string parname="D:\\Temp\\";                                  // Parent Directory
          //string dirname = TextBox1.Text;                               // Reading directory name from a textbox in the form
            string dir1 = "C:\\kneeAngleAnalyzer\\reportCapture";
 
          if(Directory.Exists(dir1))
           {
            //Response.Write("Directory Exists !");
            //Directory.Delete(dir1,true);
               return true;
           }
          else
           {
            //Response.Write("Directory Does Not Exist !! ");
               return false;
           }
        }
    }
}
